﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Input;
using System.Collections;
namespace AnotherGoddamnedTextEditor
{
    /// <summary>
    /// Assistant form for the text editor. 
    /// Intended for the purpose of displaying data that would not otherwise fit on the main form.
    /// Quite annoying to add things to because it is not modular in any sense of the word, though it should be pretty lightweight.
    /// </summary>
    public class Displayform1 :Panel
    {
        /// <summary>
        /// Sets the display for wordcount
        /// </summary>
        public string wordcount
        {
            set
            {
                this.worddisplay.Text = "Words:" + value;
            }
        }
        public string readingease
        {
            set
            {
                readingeaseresult.Text = "Flesch reading level:" + value;
            }
        }
        private SplitContainer splitpanel;
        private SplitContainer plitpanel;
        private FlowLayoutPanel flitpanel;
        private Label readingeaseresult;
        private Label worddisplay;
        private Label wordlengthdisplay;
        private Button readingeasecheck;
        /// <summary>
        /// sets the display to show the current avg length of each word.
        /// </summary>
        public string wordlength
        {
            set
            {
                wordlengthdisplay.Text = "Avg word length:" + value;
            }
        }
        public Displayform1()
        {
            readingeasecheck = new Button();
            readingeasecheck.Text = "Calculate reading ease";
            readingeasecheck.Click += new EventHandler(ReadingeasecheckClick);
            plitpanel = new SplitContainer();
            flitpanel = new FlowLayoutPanel();
            flitpanel.Dock = DockStyle.Fill;
            readingeaseresult = new Label();
            flitpanel.Controls.Add(readingeasecheck);
            flitpanel.Controls.Add(readingeaseresult);
            readingease = "";
            readingeaseresult.Dock = DockStyle.Fill;
            readingeasecheck.TextAlign = ContentAlignment.MiddleCenter;
            readingeaseresult.TextAlign = ContentAlignment.MiddleCenter;
            readingeaseresult.Dock=DockStyle.Bottom;
            plitpanel.Orientation = Orientation.Horizontal;
            plitpanel.Panel2.Controls.Add(flitpanel);
            plitpanel.Dock = DockStyle.Fill;

            this.splitpanel = new SplitContainer();
            splitpanel.Dock = DockStyle.Fill; plitpanel.Panel1.Controls.Add(splitpanel);
            splitpanel.Orientation = Orientation.Horizontal;
            worddisplay = new Label();
            wordlengthdisplay = new Label();
            splitpanel.BackColor = Color.Transparent;
            splitpanel.Panel1.Controls.Add(worddisplay);
            splitpanel.Panel1.BackColor = Color.Transparent;
            splitpanel.Panel2.Controls.Add(wordlengthdisplay);
            splitpanel.Panel2.BackColor = Color.Transparent;
            Controls.Add(plitpanel);
            //this.Controls.Add(splitpanel);
            worddisplay.Dock = DockStyle.Fill;
            worddisplay.TextAlign = ContentAlignment.MiddleCenter;
            wordlengthdisplay.Dock = DockStyle.Fill;
            wordlengthdisplay.TextAlign = ContentAlignment.MiddleCenter;
            
            

        }
        public event EventHandler CalculateButtonClicked;
        void ReadingeasecheckClick(object sender, EventArgs e)
        {
            if (CalculateButtonClicked != null)
                CalculateButtonClicked.Invoke(this, e);
        }
    }
    public partial class Form1 : Form
    {
        /// <summary>
        /// displays the wordcount, avg word length, and or anything else that I want to include
        /// </summary>
        private Displayform1 _assistant;
        private static void SplitSentences(string t, out double avgsyllable, out double avgwrdsentence)
        {
            string sSourceText = t;
            char[] arrSplitChars = { '.', '?', '!' };
            // split the sentences with a regular expression
            string[] splitSentences =
                sSourceText.Split(arrSplitChars, StringSplitOptions.RemoveEmptyEntries);
            int syllable = 0;
            string[] words = new string[Regex.Matches(sSourceText, @"[A-Za-z0-9]+").Count];
            for (int y = 0; y < Regex.Matches(sSourceText, @"[A-Za-z0-9]+").Count; y++)
                words[(int)y] = Regex.Matches(sSourceText, @"[A-Za-z0-9]+")[(int)y].ToString();
            for (var q = 0; q < words.Length; q++)
                syllable += SyllableCount(words[q]);
            double w = (double)syllable / (double)words.Length;
            double wordpersentence = 0;
            for (var r = 0; r < splitSentences.Length; r++)
                wordpersentence += Regex.Matches(splitSentences[(int)r], @"[A-Za-z0-9]+").Count;
            words = null;
            avgsyllable = w;

            avgwrdsentence = wordpersentence / splitSentences.Length;
        }
        private void Initializeform()
        {
            if(_assistant!=null)
                _assistant.Dispose();
            _assistant = new Displayform1();
            _assistant.wordcount = "";
            _assistant.wordlength = "";
            _assistant.Dock = DockStyle.Fill;
            
            _assistant.CalculateButtonClicked += new EventHandler(_assistant_CalculateButtonClicked);

        }
        void _assistant_CalculateButtonClicked(object sender, EventArgs e)
        {
            if (!ReadingCheckCalculation.IsBusy)
                ReadingCheckCalculation.RunWorkerAsync(textEditor1.Text);
        }

    }
}
